package icselab.appmodule.server;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.ReflectionException;

import org.jboss.mx.util.MBeanProxyExt;
import org.jboss.mx.util.MBeanServerLocator;
import org.jboss.mx.util.ObjectNameFactory;
import org.jboss.management.j2ee.ServletMBean;
import org.jboss.management.j2ee.WebModuleMBean;

import javax.management.j2ee.statistics.Statistic;
import javax.management.j2ee.statistics.Stats;


public class ServerManager {

	public Stats getServerStat(String moduleType, String instanceName, int servletIndex){
		//System.out.println("querying servlets of Module: "+"jboss.management.local:J2EEApplication=null,J2EEServer=Local,j2eeType="+moduleType+",name="+instanceName);
		WebModuleMBean webModuleBean = (WebModuleMBean) MBeanProxyExt.create(WebModuleMBean.class,
				ObjectNameFactory.create("jboss.management.local:J2EEApplication=null,J2EEServer=Local,j2eeType="+moduleType+",name="+instanceName));
		String[] servlets = webModuleBean.getservlets();
		
		ServletMBean statBean = (ServletMBean)MBeanProxyExt.create(ServletMBean.class,
				ObjectNameFactory.create(servlets[servletIndex]));
		return statBean.getstats();
	}
	
	public String[] getServerStat(String moduleType, String instanceName){	
		WebModuleMBean webModuleBean = null;
		try{
		webModuleBean = (WebModuleMBean) MBeanProxyExt.create(WebModuleMBean.class,
				ObjectNameFactory.create("jboss.management.local:J2EEApplication=null,J2EEServer=Local,j2eeType="+moduleType+",name="+instanceName));
		}
		catch(RuntimeException e){
			System.out.println("Error on creating jboss.management.local:J2EEApplication=null,J2EEServer=Local,j2eeType="+moduleType+",name="+instanceName);
			return new String[0];
		}
		//System.out.println("Getting monitoring statistics of instance: "+instanceName);
		String[] servlets = webModuleBean.getservlets();
		String[] statList = new String[servlets.length];
		for(int i=0;i<servlets.length;i++)
		{
			ServletMBean statBean = (ServletMBean)MBeanProxyExt.create(ServletMBean.class,
					ObjectNameFactory.create(servlets[i]));
			Statistic[] statistics = statBean.getstats().getStatistics();
			statList[i] = statistics[0].toString();	
		}
		return statList;
	}
	
	public void resetServerStat(String moduleType, String instanceName){	
		WebModuleMBean webModuleBean = (WebModuleMBean) MBeanProxyExt.create(WebModuleMBean.class,
				ObjectNameFactory.create("jboss.management.local:J2EEApplication=null,J2EEServer=Local,j2eeType="+moduleType+",name="+instanceName));
		String[] servlets = webModuleBean.getservlets();
		for(int i=0;i<servlets.length;i++)
		{
			ServletMBean statBean = (ServletMBean)MBeanProxyExt.create(ServletMBean.class,
					ObjectNameFactory.create(servlets[i]));	
			System.out.print("Reset the statistic of "+servlets[i]);
			statBean.resetStats();
		}
	}
	
	//check whether the instance has been deployed successfully
	public boolean isInstanceDeployed(String iUrl){
		try{
			Object[] parameter = {iUrl};
			String[] paraType = {"java.lang.String"};
			MBeanServer server = MBeanServerLocator.locateJBoss();
			//check if the instance is running; if running, stop it
			if ("true" == (server.invoke(ObjectNameFactory
					.create("jboss.system:service=MainDeployer"), "isDeployed",
					parameter, paraType).toString()))
			{
				return true;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return false;
	}
	
	//start the instance by instanceUrl
	public boolean startInstance(String iUrl){
		
		//check if the service is stopped; if not, no need to start
		if (this.isInstanceDeployed(iUrl))
			return true;
		
		try {
			MBeanServer server = MBeanServerLocator.locateJBoss();
			Object[] parameter = {iUrl};
			String[] paraType = {"java.lang.String"};
		
			//start the service
			server.invoke(ObjectNameFactory
					.create("jboss.system:service=MainDeployer"), "start",
					parameter, paraType);
			
			//check if the start process completed
			if (this.isInstanceDeployed(iUrl))
				return true;
			else
				return false;
			
		} catch (InstanceNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MBeanException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ReflectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	//stop the instance by instanceUrl
	public boolean stopInstance(String iUrl){
		
		//check if the service is stopped; if not, no need to start
		if (!this.isInstanceDeployed(iUrl))
			return true;
		
		try {
			MBeanServer server = MBeanServerLocator.locateJBoss();
			Object[] parameter = {iUrl};
			String[] paraType = {"java.lang.String"};
		
			//start the service
			server.invoke(ObjectNameFactory
					.create("jboss.system:service=MainDeployer"), "stop",
					parameter, paraType);
			
			//check if the start process completed
			if (!this.isInstanceDeployed(iUrl))
				return true;
			else
				return false;
			
		} catch (InstanceNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MBeanException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ReflectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
}